<?php

/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );


class getDocmanNotTab extends cbTabHandler {

	function getDocmanNotTab() {
		$this->cbTabHandler();
	}
	
	function getDisplayTab($tab,$user,$ui) {
        global $database;

		$return = null;
		
		$params = $this->params; // get parameters (plugin and related tab)
		
		$urlAddPlugin = $this->_getAbsURLwithParam(array('user' => $user->id),'addDocument');
		$id = $this->_getReqParam('id',0);
		$task = mosGetParam($_REQUEST,'task','');
		
        if ($task == 'deleteDocument' && $id > 0){
            $query = "DELETE FROM  #__docman_owners WHERE document_id = {$id} AND user_id = {$user->id}";
            $database->setquery($query);
            $database->query();
        }

        if ($task == 'addDocument' && $id > 0){
            $query = "INSERT INTO #__docman_owners VALUES ( {$id}, $user->id)";
            $database->setQuery($query);
            $database->query();
        }

        $query = "SELECT d.dmname, d.id FROM #__docman AS d LEFT JOIN  #__docman_owners AS o ON d.id = o.document_id WHERE d.published = 1 AND isnull(o.user_id) ";
        $database->setQuery($query);
        $documents = $database->loadObjectList();
        
		$return = "<form method=\"post\" action=\"{$urlAddPlugin}\">";
		$return .= "<select name=".$this->_getPagingParamName('id')." >";
		foreach ($documents as $key => $document){
            $return .= "<option Value=\"{$document->id}\" >{$document->dmname}</option>";
		}
		$return .= "</select>";
		$return .= "<input type=\"submit\" value=\"Add\">";
		$return .= "</form>";

        $query = "SELECT d.dmname, d.id FROM #__docman_owners AS o INNER JOIN #__docman AS d ON o.document_id = d.id WHERE o.user_id = ".$user->id;
        $database->setQuery($query);
        $rows = $database->loadObjectList();
        
        $return .= '<h3>Documents Owned</h3>';
        $return .= '<table>';
        foreach ($rows as $key => $row){
            $urlDeletePlugin = $this->_getAbsURLwithParam(array('id' => $row->id, 'user' => $user->id ),'deleteDocument');
            $return .= '<tr>';
            $return .= '<td>'.$row->dmname.'</td>';
            $return .= '<td><a href="'.$urlDeletePlugin.'">delete</a></td>';
            $return .= '</tr>';
        }
        $return .= '</table>';
		return $return;
	} 
} 
?>